{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据包络分析(Data Envelopment Analysis, DEA)\n",
    "\n",
    "**从最有利于决策单元的角度进行评价，从而避免了确定各指标在优先意义下的权重.**\n",
    "\n",
    "- 决策单元(Decision Making Units, DMU): 每个DMU有m种投入和s种产出.\n",
    "- 向量$X_j,\\ Y_j$分别表示决策单元$j$的输入和输出向量; $v,\\ u$分别表示输入,输出权值向量.\n",
    "- 评价单元$j$的效率评价指数为: $h_j=(u^TY_j)/(v^TX_j),\\ j=1,2,\\cdots,n$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DEA有效的学校为:  ['A' 'E' 'F']\n"
     ]
    }
   ],
   "source": [
    "# 例14.4问题\n",
    "\n",
    "import numpy as np\n",
    "import scipy.optimize\n",
    "\n",
    "school_name = np.array(['A', 'B', 'C', 'D', 'E', 'F'])\n",
    "X = np.array([[89.39, 64.3],\n",
    "              [86.25, 99],\n",
    "              [108.13, 99.6],\n",
    "              [106.38, 96],\n",
    "              [62.40, 96.2],\n",
    "              [47.19, 79.9]])\n",
    "\n",
    "Y = np.array([[25.2, 223],\n",
    "              [28.2, 287],\n",
    "              [29.4, 317],\n",
    "              [26.4, 291],\n",
    "              [27.2, 295],\n",
    "              [25.2, 222]])\n",
    "\n",
    "# 转化成线性规划问题, 用'01 线性规划.ipynb'的方法解\n",
    "DEA = []\n",
    "# 产出和投入拼起来\n",
    "A_ub = np.concatenate((Y, X), axis = 1)\n",
    "b_ub = np.zeros(A_ub.shape[0])\n",
    "for index in range(X.shape[0]):\n",
    "    # scipy的这个函数是求最小值的, 所以要取负号\n",
    "    c = -np.concatenate((Y[index], np.zeros(2)))\n",
    "    A_eq = [np.concatenate((np.zeros(2), X[index]))]\n",
    "    b_eq = [-1]\n",
    "    bounds = [(0, None), (0, None), (None, 0), (None, 0)]\n",
    "\n",
    "    res = scipy.optimize.linprog(c=c, A_ub=A_ub, b_ub=b_ub, A_eq=A_eq, b_eq=b_eq, bounds=bounds)\n",
    "    DEA.append(np.round(-res.fun, 5))\n",
    "\n",
    "DEA = np.array(DEA)\n",
    "DEA_effective = school_name[np.where(DEA == 1)]\n",
    "print('DEA有效的学校为: ', DEA_effective)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
